<!doctype html>
<head>
	<title>Tube Example</title>
	<link href="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css" rel="stylesheet">
	<script src="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js"></script>
	<script src="../dist/threebox.js" type="text/javascript"></script>
	<link href="../dist/threebox.css" rel="stylesheet" />
	<script src="config.js"></script>
	<style>
		body, html {
			width: 100%;
			height: 100%;
			margin: 0;
		}

		#map {
			width: 100%;
			height: 100%;
		}
	</style>
</head>
<body>
	<div id='map' class='map'></div>

	<script type="module">

		if(!config) console.error("Config not set! Make a copy of 'config_template.js', add in your access token, and save the file as 'config.js'.");
		
		mapboxgl.accessToken = config.accessToken;
		var origin = [-95.97299, 36.15031,0];

		var map = new mapboxgl.Map({
			container: 'map',
			style: 'mapbox://styles/mapbox/light-v10',
			center: origin,
			zoom: 14,
			pitch: 60,
			heading: 41,
			antialias: true
		});

		//generate a spiral line geometry (not essential for understanding this demo)

		var lineGeometry = [];

		for (var l = 0; l<200; l++) {

			var delta = [
				Math.sin(l/5) * l/40, 
				Math.cos(l/5) * l/40, 
				l / 10
			]

			var newCoordinate = origin.map(function(d,i){
				return d + delta[i]
			})
			lineGeometry.push(newCoordinate)
		}

		console.log("Tube's line geometry: ", lineGeometry);


		let stats;
		import Stats from 'https://threejs.org/examples/jsm/libs/stats.module.js';
		function animate() {
			requestAnimationFrame(animate);
			stats.update();
		}

		map.on('style.load', function () {
			// stats
			stats = new Stats();
			map.getContainer().appendChild(stats.dom);
			animate();

			map.addLayer({
				id: 'custom_layer',
				type: 'custom',
				renderingMode: '3d',
				onAdd: function (map, mbxContext) {

					// instantiate threebox
					window.tb = new Threebox(
						map,
						mbxContext,
						{
							defaultLights: true,
							enableSelectingObjects: true, //change this to false to disable 3D objects selection
							enableDraggingObjects: true, //change this to false to disable 3D objects drag & move once selected
							enableRotatingObjects: true, //change this to false to disable 3D objects rotation once selected
							enableTooltips: true // change this to false to disable default tooltips on fill-extrusion and 3D models
						}
					);

					var tubeOptions = {
						geometry: lineGeometry,
						radius: 0.8,
						sides: 8,
						material: 'MeshPhysicalMaterial',
						color: '#00ffff',
						anchor: 'center',
						side: THREE.DoubleSide
					}

					let tube = tb.tube(tubeOptions);
					tube.setCoords(origin);
					// tube.material.wireframe = true
					tb.add(tube);

					tube.set({ rotation: { x: 0, y: 0, z: 11520 }, duration: 20000 });

				},

				render: function (gl, matrix) {
					tb.update();
				}
			});
		});
	</script>
</body>